package com.example.peoplesecure;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Chronometer;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class MyChronometer extends Chronometer implements LifecycleObserver{

    private final String TAG = "hello";
    private long time;

    public MyChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //这个的含义就是说当执行到onResume时，这个方法就会被执行
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void startMeter(){
        Log.d(TAG, "start: nihao");
        setBase(SystemClock.elapsedRealtime() - time);
        start();
        int b = 1;
        int c = 2;
    }
    //这个的含义就是说当执行到onPause时，这个方法就会被执行
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void stopMeter(){
        Log.d(TAG, "stop: yuanzhihua");
        time = SystemClock.elapsedRealtime() - getBase();
        stop();
    }
}
